package com.freemalll.system.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.freemalll.common.core.utils.poi.ExcelUtil;
import com.freemalll.common.core.web.controller.BaseController;
import com.freemalll.common.core.web.domain.AjaxResult;
import com.freemalll.common.core.web.page.TableDataInfo;
import com.freemalll.common.log.annotation.Log;
import com.freemalll.common.log.enums.BusinessType;
import com.freemalll.common.security.annotation.RequiresPermissions;
import com.freemalll.common.security.utils.SecurityUtils;
import com.freemalll.system.domain.SysResources;
import com.freemalll.system.service.ISysResourcesService;

/**
 * 系统资源 信息操作处理
 * 
 * @author freemalll
 */
@RestController
@RequestMapping("/resources")
public class SysResourcesController extends BaseController
{
    @Autowired
    private ISysResourcesService resourcesService;

    /**
     * 获取系统资源列表
     */
    @RequiresPermissions("system:resources:list")
    @GetMapping("/list")
    public TableDataInfo list(SysResources resources)
    {
        startPage();
        List<SysResources> list = resourcesService.selectResourcesList(resources);
        return getDataTable(list);
    }

    @Log(title = "系统资源", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:resources:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysResources resources)
    {
        List<SysResources> list = resourcesService.selectResourcesList(resources);
        ExcelUtil<SysResources> util = new ExcelUtil<SysResources>(SysResources.class);
        util.exportExcel(response, list, "系统资源数据");
    }

    /**
     * 根据系统资源编号获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id)
    {
        return success(resourcesService.selectResourcesById(id));
    }

    /**
     * 根据资源编码查询系统资源信息
     */
    @GetMapping(value = "/resourceCode/{resourceCode}")
    public AjaxResult getInfoByResourceCode(@PathVariable String resourceCode)
    {
        return success(resourcesService.selectResourcesByResourceCode(resourceCode));
    }

    /**
     * 根据父级应用id查询子资源
     */
    @GetMapping(value = "/parent/{parentId}")
    public AjaxResult getInfoByParentId(@PathVariable String parentId)
    {
        return success(resourcesService.selectResourcesByParentId(parentId));
    }

    /**
     * 根据应用编码查询资源列表
     */
    @GetMapping(value = "/appCode/{appCode}")
    public AjaxResult getInfoByAppCode(@PathVariable String appCode)
    {
        return success(resourcesService.selectResourcesByAppCode(appCode));
    }

    /**
     * 新增系统资源
     */
    @RequiresPermissions("system:resources:add")
    @Log(title = "系统资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysResources resources)
    {
        if (!resourcesService.checkResourceCodeUnique(resources))
        {
            return error("新增资源'" + resources.getResourceName() + "'失败，资源编码已存在");
        }
        resources.setCreateName(SecurityUtils.getUsername());
        return toAjax(resourcesService.insertResources(resources));
    }

    /**
     * 修改系统资源
     */
    @RequiresPermissions("system:resources:edit")
    @Log(title = "系统资源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysResources resources)
    {
        if (!resourcesService.checkResourceCodeUnique(resources))
        {
            return error("修改资源'" + resources.getResourceName() + "'失败，资源编码已存在");
        }
        resources.setUpdateName(SecurityUtils.getUsername());
        return toAjax(resourcesService.updateResources(resources));
    }

    /**
     * 删除系统资源
     */
    @RequiresPermissions("system:resources:remove")
    @Log(title = "系统资源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        resourcesService.deleteResourcesByIds(ids);
        return success();
    }

    /**
     * 获取资源树选择列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysResources resources)
    {
        List<SysResources> resourcesList = resourcesService.selectResourcesList(resources);
        return success(resourcesService.buildResourcesTreeSelect(resourcesList));
    }
}